package com.shushan.home.adapter;

import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shushan.pages.bean.ExerciseRecordListBean;
import com.shushan.shushanbao.R;
import com.shushan.ui.CustomRoundProgressView;

import java.util.List;

/**
 * @ClassName: ExRecordAdapter
 * @Desciption: //联系记录GridView适配器
 * @author: zhangshihao
 * @date: 2018-09-19
 */
public class ExRecordAdapter extends BaseAdapter{

    private static final String TAG = ExRecordAdapter.class.getSimpleName();

    private List<ExerciseRecordListBean> recordListBeans;
    private int[] imgIds = {R.drawable.ic_sync_ex_tag, R.drawable.ic_sw_ex_tag, R.drawable.ic_speaking_ex_tag,
            R.drawable.ic_ew_ex_tag, R.drawable.ic_hearing_ex_tag, R.drawable.ic_oc_ex_tag, R.drawable.ic_znlx_ex_tag,
            R.drawable.ic_yw_zxlx_ex_tag,R.drawable.ic_sx_zxlx_ex_tag,R.drawable.ic_yy_zxlx_ex_tag};
    private int[] progressColors = {Color.parseColor("#FF7954"), Color.parseColor("#FFCB2E"), Color.parseColor("#58E274"),
            Color.parseColor("#6CCBFF"), Color.parseColor("#CF95FF"), Color.parseColor("#FF769A"), Color.parseColor("#FFCB2E"),
            Color.parseColor("#FF7954"), Color.parseColor("#6CCBFF"),Color.parseColor("#58E274")};
    private int[] proBgColors = {Color.argb(38, 255, 121, 84), Color.argb(38, 255, 203, 46), Color.argb(38, 88, 226, 116),
            Color.argb(38, 108, 203, 255), Color.argb(38, 207, 149, 255), Color.argb(38, 255, 188, 154), Color.argb(38, 255, 203, 46),
            Color.argb(38, 255, 121, 84), Color.argb(38, 108, 203, 255),Color.argb(38, 88, 226, 116)};

    public ExRecordAdapter(List<ExerciseRecordListBean> recordListBeans) {
        this.recordListBeans = recordListBeans;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_exercise_record,null);
            holder = new ViewHolder();
            holder.ivTag = convertView.findViewById(R.id.iv_tag);
            holder.exCount = convertView.findViewById(R.id.tv_ex_count);
            holder.exTopicCount = convertView.findViewById(R.id.tv_ex_topic_count);
            holder.rightRateView = convertView.findViewById(R.id.crpv_right_rate);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }
        holder.ivTag.setImageResource(imgIds[position]);
        holder.rightRateView.setmProgressRoundColor(progressColors[position]);
        holder.rightRateView.setmProgressRoundBgColor(proBgColors[position]);
        ExerciseRecordListBean listBean = recordListBeans.get(position);
        holder.exCount.setText("练习"+listBean.answer_num+"次");
        holder.exTopicCount.setText("练习"+listBean.exercise_num+"题");
        int percent = 0;
        float rate = (float) listBean.right_num / listBean.exercise_num * 100;
        if(rate > 0 && rate < 1){
            percent = 1;
        }else{
            percent = (int)rate;
        }

        holder.rightRateView.setMax(100);
        holder.rightRateView.setProgress(percent);

        return convertView;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getCount() {
        return recordListBeans.size();
    }

    class ViewHolder{
        ImageView ivTag;
        TextView exCount;
        TextView exTopicCount;
        CustomRoundProgressView rightRateView;
    }

}
